<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter
 *
 * An open source application development framework for PHP 5.1.6 or newer
 *
 * @package		CodeIgniter
 * @author		ExpressionEngine Dev Team
 * @copyright	Copyright (c) 2008 - 2011, EllisLab, Inc.
 * @license		http://codeigniter.com/user_guide/license.html
 * @link		http://codeigniter.com
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * CodeIgniter Model Class
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		ExpressionEngine Dev Team
 * @link		http://codeigniter.com/user_guide/libraries/config.html
 */
class CI_Model {

    protected $_instance = array(
        'table' => '',
        'primaryKey' => '',
    );
    public $_data = null;

	/**
	 * Constructor
	 *
	 * @access public
	 */
	function __construct()
	{
		log_message('debug', "Model Class Initialized");
	}

	/**
	 * __get
	 *
	 * Allows models to access CI's loaded classes using the same
	 * syntax as controllers.
	 *
	 * @access private
	 */
	function __get($key)
	{
		$CI =& get_instance();
		return $CI->$key;
	}

    public function exist_data($field, $value) {

    }

    public function get_field_by_id($id, $field) {
        $query = $this->db->query('SELECT ' . $field . ' FROM ' . $this->_instance['table'] . ' WHERE ' . $this->_instance['primaryKey'] . ' = ' . $id . ' LIMIT 0, 1');
        $row = $query->row();
        $query->free_result();
        if (empty($row) || !is_object($row)) {
            return null;
        }
        return $row->$field;
    }

    public function get_collection($start, $limit, $conds = null, $orders = null) {
        $query = 'SELECT * FROM ' . $this->_instance['table'];
        if (is_array($conds)) {
            $conds = implode(' AND ', $conds);
        }
        if (!empty($conds)) {
            $query .= ' WHERE ' . $conds;
        }
        if (!empty($orders)) {
            $query .= ' ORDER BY ' . $orders;
        }
        $query .= ' LIMIT ' . $start . ', ' . $limit;
        $this->_query = $query;
        $query = $this->db->query($query);
        $list_product = array();
        foreach ($query->result() as $row) {
            $row->data = unserialize($row->data);
            $list_product[] = $row;
        }
        $query->free_result();
        return $list_product;
    }

    public function get_core() {
        $CI =& get_instance();
        return $CI;
    }

    public function save_session_data($key, $value) {
        $CI = $this->get_core();
        $CI->st_registry->register($key, serialize($value));
        return $this;
    }

    public function get_session_data($key) {
        $CI = $this->get_core();
        $data = $CI->st_registry->registry($key);
        if (!empty($data)) {
            @$data = unserialize($data);
        }
        return $data;
    }
}
// END Model Class

/* End of file Model.php */
/* Location: ./system/core/Model.php */